﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LibrarySolution.ComMethod;
using LibrarySolution.DAO.委托;
using LibrarySolution.DAO.基础;
using LibrarySolution.Model.基础;

namespace LibrarySolution.Frm
{
    public partial class FrmJyts : Form
    {
        #region 属性
        JHBkDao jhbkDao = new JHBkDao();
        JSRecord jsRecord;
        public DataTable datatable;
        YhDao yhDao;
        String yhNo;
        private static FrmJyts Instance;
        Delegates delegates = new Delegates();
        #endregion

        #region 构造器
        public FrmJyts()
        {
            InitializeComponent();
            delegates.refreshLBEvent += new Delegates.refreshLBEventHandler(BindingPerInfo);
            UCDataQuery.DataQuery +=new ComControl.UCDataQuery.dataQueryEventHandler(UCDataQuery_DataQuery);
        }
        #endregion

        #region 事件
        public static FrmJyts GetInstance()
        {
            if (Instance == null || Instance.IsDisposed)
            {
                Instance = new FrmJyts();
                Instance.jsRecord = new JSRecord();
                Instance.MdiParent = GlobalVar.frmMain;
                Instance.BindingLiBook();
                Instance.DefaultPerInfo();

                GlobalVar.frmMain.panel1.Controls.Add(Instance);
                GlobalVar.FrmJs = Instance;
            }
            Instance.BringToFront();
            return Instance;
        }

        private void TstripBtnRefresh_Click(object sender, EventArgs e)
        {
            BindingLiBook();
            BindingPerInfo();
        }

        private void TstripBtnSearch_Click(object sender, EventArgs e)
        {
            SearchPer();
        }

        private void TstripBtnJS_Click(object sender, EventArgs e)
        {
            if (groupBox1.Text.Length <= 5) return;

            if (ifgivebake())
            {
                if (int.Parse(GlobalVar.YhxxEntity.yh_yjsl) == 20)
                {
                    MessageBox.Show("您已借书20本,不能再借");
                    return;
                }
                if (UCDataQuery.rowDataBase() == null) return;
                String bkName, bkNo;
                bkName = UCDataQuery.rowDataBase()[1];
                bkNo = UCDataQuery.rowDataBase()[0];
                if (MessageBox.Show("您要借的书是《" + bkName + "》?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation,
                    MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.Yes)
                {
                    jsRecord = new JSRecord();
                    GlobalVar.YhxxEntity.yh_xjsl = GlobalVar.YhxxEntity.yh_xjsl;
                    GlobalVar.YhxxEntity.yh_yjsl = (int.Parse(GlobalVar.YhxxEntity.yh_yjsl) + 1).ToString();
                    jsRecord.js_ghrq = DataUtil.addTime(GlobalVar.YhxxEntity.jyqx);
                    jsRecord.js_yhno = groupBox1.Text.Substring(5);
                    jsRecord.js_tsbh = bkNo;
                    jsRecord.js_yhxj = GlobalVar.YhxxEntity.yh_xjsl;
                    jsRecord.js_yhyj = GlobalVar.YhxxEntity.yh_yjsl;
                    jsRecord.js_time = DataUtil.getTime();
                    jhbkDao.insertJs(jsRecord);
                    BindingLiBook();
                    BindingPerInfo();
                }
            }
        }

        private void FrmJS_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!GlobalVar.YhEnter)
                GlobalVar.YhxxEntity.yh_no = null;
        }

        private void TxtQueryByBkAuthor_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                DataTable dt = null;
                jsRecord = new JSRecord();
                jsRecord.js_writer = TxtQueryByBkAuthor.Text.Trim();
                dt = jhbkDao.getBkxxTable(jsRecord);
                if (dt == null || dt.Rows.Count == 0)
                    MessageBox.Show("输入有误或书本不存在或已外借!");
                else
                {
                    UCDataQuery.Datasource = dt;
                    UCDataQuery.Refresh();
                }
                TxtQueryByBkAuthor.Text = "";
            }
        }

        private void TxtQueryByBkName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                jsRecord = new JSRecord();
                jsRecord.js_tsmc = TxtQueryByBkName.Text.Trim();
                DataTable dt = null;
                dt = jhbkDao.getBkxxTable(jsRecord);
                if (dt == null || dt.Rows.Count == 0)
                    MessageBox.Show("输入有误或书本不存在或已外借!");
                else
                {
                    UCDataQuery.Datasource = dt;
                    UCDataQuery.Refresh();
                }
                TxtQueryByBkName.Text = "";
            }
        }

        private void FrmJyts_Load(object sender, EventArgs e)
        {
            Instance.TxtQueryByYhno.Focus();
        }

        private void TxtQueryByBkNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                jsRecord = new JSRecord();
                jsRecord.js_tsbh = TxtQueryByBkNo.Text.Trim();
                DataTable dt = null;
                dt = jhbkDao.getBkxxTable(jsRecord);
                if (dt == null || dt.Rows.Count == 0)
                    MessageBox.Show("输入有误或书本不存在或已外借!");
                else
                {
                    UCDataQuery.Datasource = dt;
                    UCDataQuery.Refresh();
                }
                TxtQueryByBkNo.Text = "";
            }
        }        
        
        private void TxtQueryByYhno_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar) || e.KeyChar == '\b'))
                e.Handled = true;
            if (e.KeyChar == 13)
            {
                SearchPer();
            }
        }
        #endregion

        #region 方法

        void BindingLiBook()
        {
            DataTable datetable = new DataTable();
            jsRecord = new JSRecord();
            groupBox4.Text = "馆藏图书共:" + jhbkDao.getBkCount() + "本";
            datetable = jhbkDao.getBkxxTable(jsRecord);
            UCDataQuery.Datasource = datetable;
            UCDataQuery.Refresh();
        }

        DataTable  UCDataQuery_DataQuery()
        {
            JSRecord jsRecord = new JSRecord();
            jsRecord.js_tsmc = ComControl.UCDataQuery.Query;
            return jhbkDao.getBkTbByQuery(jsRecord);
        }

        public Boolean ifgivebake()
        {
            Boolean GiveMoney = false;
            Int32 num = new int();
            DataTable dataTable;
            dataTable = jhbkDao.getTsjl(jsRecord);
            num = dataTable.Rows.Count;
            for (int i = 0; i < num; i++)
            {
                DateTime dt = DateTime.Parse(dataTable.Rows[i][8].ToString());
                if (dt < DateTime.Now && dataTable.Rows[i][10].ToString() == "0")
                {
                    if (dataTable.Rows[i]["图书状态"].ToString() != "丢失"&&
                        dataTable.Rows[i]["是否已还"].ToString()=="0")
                    {
                        MessageBox.Show("请先归还超期图书");
                        return false;
                    }
                    if (dataTable.Rows[i]["是否扣费"].ToString() == "0" &&
                        dataTable.Rows[i]["图书状态"].ToString() == "在库")
                    {
                        GiveMoney = true;
                    }
                }
                if (dataTable.Rows[i]["丢失扣费"].ToString() == "0" &&
                    dataTable.Rows[i]["图书状态"].ToString() == "丢失")
                {
                    MessageBox.Show("请先结清丢失图书的欠款");
                    return false;
                }
            }
            if (GiveMoney == true) { MessageBox.Show("请先结清超期图书的欠款"); return false; }
            return true;
        }

        void DefaultPerInfo()
        {
            LblYhName.Text = "用 户 名:";
            LblYhXjsl.Text = "续借次数:";
            LblYhYjsl.Text = "已借数量:";
            LblYhjb.Text = "用户级别:";
            LblJyqx.Text = "借阅期限:";
            LblXjqx.Text = "续借期限:";
            groupBox1.Text = "用户编号:";
            LblYhye.Text = "用户余额:";
            dataGridView1.DataSource = null;
            GlobalVar.YhxxEntity = null;
        }

        void BindingPerInfo()
        {
            jsRecord = new JSRecord();
            if (String.IsNullOrEmpty(GlobalVar.YhxxEntity.yh_no))
                jsRecord.js_yhno = groupBox1.Text.Substring(5);
            else
                jsRecord.js_yhno = GlobalVar.YhxxEntity.yh_no;
            DataTable JsdataTable = new DataTable();
            JsdataTable = jhbkDao.getMybook(jsRecord);
            LblYhName.Text = "用 户 名:" + GlobalVar.YhxxEntity.yh_name;
            LblYhXjsl.Text = "续借次数:" + GlobalVar.YhxxEntity.yh_xjsl;
            LblYhYjsl.Text = "已借数量:" + GlobalVar.YhxxEntity.yh_yjsl;
            LblYhjb.Text = "用户级别:" + GlobalVar.YhxxEntity.yh_JB;
            LblJyqx.Text = "借阅期限:" + GlobalVar.YhxxEntity.jyqx + "个月";
            LblXjqx.Text = "续借期限:" + GlobalVar.YhxxEntity.xjqx + "天";
            if(!String.IsNullOrEmpty( GlobalVar.YhxxEntity.yh_no))
                groupBox1.Text = "用户编号:" + GlobalVar.YhxxEntity.yh_no;
            LblYhye.Text = "用户余额:" + GlobalVar.YhxxEntity.yh_zhye + "元";
            dataGridView1.DataSource = JsdataTable;
            dataGridView1.Columns["记录编码"].Visible = false;
            dataGridView1.Refresh();
        }

        void SearchPer()
        {
            yhDao = new YhDao();
            yhNo = this.TxtQueryByYhno.Text;
            GlobalVar.YhxxEntity = yhDao.getYhxxByNo(yhNo, "1");

            TxtQueryByYhno.Text = "";
            if (GlobalVar.YhxxEntity.yh_no != null && GlobalVar.YhxxEntity.yh_code != null)
            {
                BindingPerInfo();
            }
            else
            {
                MessageBox.Show("用户不存在");
                DefaultPerInfo();
            }

        }
        #endregion




    }
}
